package com.hnust.root.web.controller.forward.user;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import com.hnust.root.common.base.exception.BusinessException;
import com.hnust.root.common.base.response.BaseRestController;
import com.hnust.root.common.base.response.BaseResult;
import com.hnust.root.dao.entity.domain.forward.University;
import com.hnust.root.service.forword.wx.UniversityService;
import com.hnust.root.service.forword.wx.WxUserInfoService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/user")
public class SearchUniversityController extends BaseRestController {

    @Autowired
    private UniversityService universityService;

    @Autowired
    private WxUserInfoService wxUserInfoService;

    @ApiOperation("通过学校名称模糊搜索学校")
    @GetMapping("/getUniversity")
    public BaseResult<List<University>> getUniversityByName(@RequestParam("wd") String name) {
            return fillSuccessData(universityService.getUniversityByName(name));
    }

    @ApiOperation("更新用户的大学")
    @PostMapping("/updateUniversity")
    public BaseResult updateUniversity(@RequestBody JSONObject jsonObject) throws BusinessException {
        String openId = getWxAccountOpenId();
        String key = "university";
        String value = jsonObject.getStr(key);
        if (!StrUtil.isEmpty(value)) {
            boolean result = wxUserInfoService.updateUserInfo(openId, key, value);
            return result ? fillSuccessData() : fillFailData();
        } else {
            throw new BusinessException("参数错误");
        }

    }


}
